<?php
class ProductController extends Zend_Controller_Action{
	public $_model;
	
	public function init(){

		$this->_model = new Application_Model_DbTable_Product();
		
	}
	
	public function listAction(){
		// var_dump($_COOKIE);
		// var_dump($_SESSION);
		// echo session_id();
		$this->view->layout()->placeholders = array('slider'=>array('status'=>false,'content'=>""));
		$filters = array();
		
		//từ khóa
		$keyword = $this->_request->getParam('keyword','');
		$filters['keyword']=$keyword;
		$this->view->keyword = $keyword;
		if(isset($_GET['keyword'])){
			$this->view->title=$this->view->translate('Kết quả tìm kiếm');
		}
        
		//phân loại
		$category = $this->_request->getParam('category',0);
		$filters['category']=$category;
		$this->view->category = $category;

        
		//hang san xuat		
		$manufacture = $this->_request->getParam('manufacture',0);
		$filters['manufacture']=$manufacture;
		$this->view->manufacture = $manufacture;
		
		//từ giá
		$startPrice = $this->_request->getParam('from-price',null);
		$filters['startPrice']=$startPrice;
		$this->view->startPrice = $startPrice;
		
		//đến giá
		$endPrice = $this->_request->getParam('to-price',null);
		$filters['endPrice']=$endPrice;
		$this->view->endPrice = $endPrice;
		
		// từ ngày
		$fromDate = $this->_request->getParam('fromDate',null);
		$filters['fromDate']=Cms_String_String::getDateFromString($fromDate);
		$this->view->fromDate = $fromDate;
		
		// đến ngày
		$toDate = $this->_request->getParam('toDate',null);
		$filters['toDate']=Cms_String_String::getDateFromString($toDate,'last');//chuyển về định dạng của mysql
		$this->view->toDate = $toDate;
		
        
		// trạng thái | status
		$status = $this->_request->getParam('searchStatus',"");
		$filters['status']=$status;
		$this->view->status=$status;
		
		// trạng thái hãng sản xuất		
		$filters['manufactureStatus']=1;
		
        //san pham hot
        $hot = $this->_request->getParam('hot');
        
		
        
        
		// sắp xếp | order
		$order = $this->_request->getParam('order',"");
        
		if($order!=""){
			list($orderBy,$orderType) = explode('-', $order);
		}else{
			$orderBy=null;
			$orderType=null;
		}
		
		
		//get item count per page
		$icpp = $this->_request->getParam('icpp',12);
		
		//get current page number
		$currentPage = $this->_request->getParam('page',1);
		
        //get producg from database
		$list = $this->_model->getList($filters,$orderType,$orderBy);
		
		// pagination
		$paginator = Zend_Paginator::factory($list);
		$paginator->setCurrentPageNumber($currentPage);
		$paginator->setItemCountPerPage($icpp);
		$paginator->setPageRange(5);
		
		//assign to view
		$this->view->paginator=$paginator;
		
		$this->view->currentTime = time();
		
		$this->view->countItem = $paginator->getTotalItemCount();
	}
	
	public function indexAction(){
		
		//san pham hot		
		$selectHot = $this->_model->select()->where('product_hot = 1')->where('product_status = 1')
		->order('product_creation_time Desc')->limit(8,0);
		$productHot = $this->_model->fetchAll($selectHot)->toArray();
		$this->view->productHot = $productHot;
		
		//san pham moi nhat
		$select = $this->_model->select()->where('product_status = 1')->order('product_creation_time DEsc')->limit(8,0);
		$productNew = $this->_model->fetchAll($select)->toArray();
		$this->view->productNew = $productNew;
		
		$select = $this->_model->select()
			->where('product_status = 1')
			->where('product_saleoff_status = 1')
			->order('product_creation_time desc')->limit(8,0);
		$productSaleOff = $this->_model->fetchAll($select)->toArray();
		$this->view->productSaleOff = $productSaleOff;
	}
	
	public function detailAction(){
        
		$this->view->layout()->placeholders = array('slider'=>array('status'=>false,'content'=>""));
		$this->view->headScript()->appendFile(PUBLIC_RESOURCE.'/skins/default/js/elevatezoom/jquery.elevatezoom.js');
		$this->view->headScript()->appendFile(PUBLIC_RESOURCE.'/skins/admin/js/fancyBox/source/jquery.fancybox.js');
		$this->view->headScript()->appendFile(PUBLIC_RESOURCE.'/skins/admin/js/fancyBox/lib/jquery.mousewheel-3.0.6.pack.js');
		$this->view->headScript()->appendFile(PUBLIC_RESOURCE.'/skins/admin/js/fancyBox/lib/jquery.mousewheel-3.0.6.pack.js');
		
		$this->view->headScript()->appendFile(PUBLIC_RESOURCE.'/skins/default/js/jquery.jcarousel.js');
		$this->view->headScript()->appendFile(PUBLIC_RESOURCE.'/skins/default/js/jcarousel.responsive.js');
		$this->view->headLink()->appendStylesheet(PUBLIC_RESOURCE.'/skins/admin/js/fancyBox/source/jquery.fancybox.css');
		$this->view->headLink()->appendStylesheet(PUBLIC_RESOURCE.'/skins/default/css/jcarousel.responsive.css');
		//lay ma san pham		
		$id = $this->_request->getParam('id');
		$select = $this->_model->select()->where('product_id = ?',$id);
		$row = $this->_model->fetchRow($select);
		$error = null;
		if($row){
			$this->view->product = $row;
			
			// get manufacutre's name
			//$manufactureMD = new Application_Model_DbTable_Manufacture();			
			//$this->view->manufactureName = $manufactureMD->find($row->product_manufacture_id)->current()['manufacture_name'];	
			
			//get product's image
			//$mdProductImage = new Application_Model_DbTable_ProductImage();
			//$this->view->images = $mdProductImage->fetchAll('product_image_product_id = '.$row->product_id);
			
			//title
			$this->view->headTitle()->prepend($row['product_alias']);	
			
			
		}else{
			$error = "mã sản phẩm không tồn tại!";
			$this->view->error = $error;
			
		}
	}
    
    public function hotAction(){
        
        $filters = array();
        $filters['hot']=1;
        // sắp xếp | order
		$order = $this->_request->getParam('order',"");
                
		if($order!=""){
			list($orderBy,$orderType) = explode('-', $order);
		}else{
			$orderBy=null;
			$orderType=null;
		}
        //get item count per page
		$icpp = $this->_request->getParam('icpp',12);
		
		//get current page number
		$currentPage = $this->_request->getParam('page',1);
		
		$list = $this->_model->getList($filters,$orderType,$orderBy);
		
		// pagination
		$paginator = Zend_Paginator::factory($list);
		$paginator->setCurrentPageNumber($currentPage);
		$paginator->setItemCountPerPage($icpp);
		$paginator->setPageRange(5);
		
		//assign to view
		$this->view->paginator=$paginator;
        
        $this->view->title= 'Sản phẩm hot';
        $this->_helper->viewRenderer->setRender('list');
		
		$this->view->countItem = $paginator->getTotalItemCount();
    }
    
     public function saleoffAction(){
        
        $filters = array();
        $filters['saleoff']=1;
        $filters['saleoff_continue']='yes';
        // sắp xếp | order
		$order = $this->_request->getParam('order',"");
                
		if($order!=""){
			list($orderBy,$orderType) = explode('-', $order);
		}else{
			$orderBy=null;
			$orderType=null;
		}
        //get item count per page
		$icpp = $this->_request->getParam('icpp',12);
		
		//get current page number
		$currentPage = $this->_request->getParam('page',1);
		
		$list = $this->_model->getList($filters,$orderType,$orderBy);
		
		// pagination
		$paginator = Zend_Paginator::factory($list);
		$paginator->setCurrentPageNumber($currentPage);
		$paginator->setItemCountPerPage($icpp);
		$paginator->setPageRange(5);
		
		//assign to view
		$this->view->paginator=$paginator;
        
        $this->view->title= 'Sản phẩm khuyến mãi';
        $this->_helper->viewRenderer->setRender('list');
		
		$this->view->countItem = $paginator->getTotalItemCount();
    }
}